package com.shioirikukuri.controller;

import com.shioirikukuri.entity.CouponType;
import com.shioirikukuri.service.ICouponTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/coupon-types")
public class CouponTypeController {
	@Autowired
	private ICouponTypeService couponTypeService;

	/**
	 * 创建优惠券类型
	 *
	 * @param couponType 优惠券类型信息
	 * @return 创建成功的优惠券类型
	 */
	@PostMapping
	public ResponseEntity<CouponType> createCouponType(@RequestBody CouponType couponType) {
		couponTypeService.createCouponType(couponType);
		return ResponseEntity.ok(couponType);
	}

	/**
	 * 根据ID查询优惠券类型
	 *
	 * @param id 优惠券类型ID
	 * @return 查询到的优惠券类型
	 */
	@GetMapping("/{id}")
	public ResponseEntity<CouponType> getCouponTypeById(@PathVariable Long id) {
		CouponType couponType = couponTypeService.getCouponTypeById(id);
		return ResponseEntity.ok(couponType);
	}

	/**
	 * 查询所有优惠券类型
	 *
	 * @return 所有优惠券类型列表
	 */
	@GetMapping
	public ResponseEntity<List<CouponType>> getAllCouponTypes() {
		List<CouponType> couponTypes = couponTypeService.getAllCouponTypes();
		return ResponseEntity.ok(couponTypes);
	}

	/**
	 * 更新优惠券类型信息
	 *
	 * @param id         优惠券类型ID
	 * @param couponType 优惠券类型信息
	 * @return 更新后的优惠券类型
	 */
	@PutMapping("/{id}")
	public ResponseEntity<CouponType> updateCouponType(@PathVariable Long id, @RequestBody CouponType couponType) {
		couponType.setId(id); // 确保ID一致
		couponTypeService.updateCouponType(couponType);
		return ResponseEntity.ok(couponType);
	}

	/**
	 * 根据ID删除优惠券类型
	 *
	 * @param id 优惠券类型ID
	 * @return 删除成功响应
	 */
	@DeleteMapping("/{id}")
	public ResponseEntity<Void> deleteCouponTypeById(@PathVariable Long id) {
		couponTypeService.deleteCouponTypeById(id);
		return ResponseEntity.noContent().build();
	}
}
